package sample.register.service;

import com.google.common.io.Files;
import org.springframework.stereotype.Service;
import sample.constants.FilesIndexConstants;
import sample.login.User;
import sample.utils.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * @author taowenchen
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Override
    public boolean register(User user) throws IOException {

        final File file = new File(FileUtils.getFile() + String.format(FilesIndexConstants.ACCOUNT_FILE_DIR, user.getAccount() + ".dat"));
        if (file.exists()) {
            return false;
        }
        file.getParentFile().mkdirs();
        Files.write(user.getPassword().getBytes("UTF-8"), file);
        return true;

    }
}
